import {
    reqUpdateMark
} from '../../../../request/api'
import {
    showMsg
} from '../../../../utils/config.js'

Page({
    data: {
        tupian: '/statics/images/xiaofang.png',
        top: 0,
        left: 0,
        id: "",
        mark: "",
        flag: false,
        y: 0,
        x: 0,
        flagadd: false,
        info: {},
        imgClass: '',
    },
    onLoad(options) {
        // console.log(options);
        if (options.mark != '') {
            this.setData({
                id: options.id,
                left: options.mark.slice(0, options.mark.indexOf(',')),
                top: options.mark.slice(options.mark.indexOf(',') + 1),
                tupian: options.image,
                info: JSON.parse(options.info),
                flag: true,
                flagadd: true,
            })
        } else {
            this.setData({
                id: options.id,
                mark: '',
                tupian: options.image,
                flag: false,
                flagadd: false,
            })
        }

        let imgClass = 'iconfont icon-smartshebeiguanli2'
        if (JSON.parse(options.info).productName == '门磁') {
            imgClass = 'iconfont icon-smartmenci'
        } else if (JSON.parse(options.info).productName == '烟感探测报警器') {
            imgClass = 'iconfont icon-smartyangan'
        } else if (JSON.parse(options.info).productName == '红外探测器') {
            imgClass = 'iconfont icon-smartrentihongwai'
        } else if (JSON.parse(options.info).productName == '智能燃气报警器') {
            imgClass = 'iconfont icon-smartreshuiqi'
        }
        this.setData({
            imgClass: imgClass,
        })
    },

    // 移动的偏移量
    binddragging(e) {
        // console.log(e);
        this.setData({
            x: e.detail.scrollLeft,
            y: e.detail.scrollTop,
        })
    },
    //绘制标记
    setPoint(e) {
        // console.log(e);
        this.setData({
            top: e.detail.y - 85 + this.data.y,
            left: e.detail.x - 10 + this.data.x,
            flag: true,
            flagadd: true,
        })
        //     wx.createSelectorQuery().select('#the-id').boundingClientRect(function(rect){
        //         console.log(rect);
        //         console.log(rect.left  );
        //   }).exec()
    },
    async addbind() {
        // console.log({x: 248.5, y: 105.75});
        const res = await reqUpdateMark({
            id: this.data.id,
            mark: parseFloat(this.data.left).toFixed(1) + ',' + parseFloat(this.data.top).toFixed(1)
        })
        if (res.code !== 20000) return showMsg(res.message)
        showMsg('标记成功！')

        let pages = getCurrentPages(); //获取当前页面pages里的所有信息。
        let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
        prevPage.setData({ // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
            'deviceInfo.mark': this.data.left + ',' + this.data.top
        })

        setTimeout(() => {
            wx.navigateBack()
        }, 1500)
    },

    onShow() {

    },

})